﻿using System;
using System.Windows.Forms;
using CRMS.Models;
using CRMS.Views.TeacherMgnt;

namespace CRMS.Views.CourseMgnt
{
    public partial class frmCourseMgnt : Form
    {
        //Field datas
        private DepartmentRep departRep = new DepartmentRep();
        private CourseRep courseRep = new CourseRep();
        //Constructor
        public frmCourseMgnt()
        {
            InitializeComponent();
            CenterToParent();
        }
        public frmCourseMgnt(string departmentId)
        {
            InitializeComponent();
            this.departmentId = departmentId;
        }
       
        private void frmCourseMgnt_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            if (this.departmentId == string.Empty)
            {
                this.dataGridView1.DataSource = courseRep.GetCourses();//2010.6.17 21：12修改
            }
            else
            {
                this.dataGridView1.DataSource = courseRep.GetCoursesByDID(this.departmentId);
            }
        }
        //Event handlers
        //选择在结果中搜索事件
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                TstripCboxCondition.Enabled = false;
            }
            else
            {
                TstripCboxCondition.Enabled = true;
            }
        }
        //查看所有开设该课程的教师信息事件
        private void btnViewDetail_Click(object sender, EventArgs e)
        {
            if (frmMain.fm.CheckForm("frmTeacherInfo"))
            {
                return;
            }
            else
            {
                frmTeacherInfo openCourseTeachers = new frmTeacherInfo(this.txtCourseId.Text, String.Empty);
                openCourseTeachers.MdiParent = frmMain.fm;
                this.Close();
                openCourseTeachers.Show();
            }
        }

        private void TstripBtnAdd_Click(object sender, EventArgs e)
        {
            txtCourseId.ReadOnly = false;
            txtCourseId.Text = String.Empty;
            txtCourseName.ReadOnly = false;
            txtCourseName.Text = String.Empty;
            comboBoxDepName.Enabled = true;
            comboBoxDepName.DataSource = departRep.GetDepartments();
            comboBoxDepName.DisplayMember = "DepartmentName";
            comboBoxDepName.ValueMember = "DepartmentId";
            comboBoxCseCat.Enabled = true;
            txtCourseCredit.ReadOnly = false;
            txtCourseCredit.Text = String.Empty;

        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            txtCourseId.ReadOnly = true;
            txtCourseName.ReadOnly = false;
            txtCourseCredit.ReadOnly = false;
            comboBoxDepName.Enabled = true;
            comboBoxCseCat.Enabled = true;
            this.TstripBtnSave.Click -= new EventHandler(this.TstripBtnSave_Click);
            this.TstripBtnSave.Click += new EventHandler(this.TstripBtnSave_Change);
            //设置下拉框
            comboBoxDepName.DataSource = departRep.GetDepartments();
            comboBoxDepName.DisplayMember = "DepartmentName";
            comboBoxDepName.ValueMember = "DepartmentId";
        }
        private void TstripBtnSave_Change(object sender, EventArgs e)
        {
            setDefualtStatus();
            Course course = new Course();
            course = courseRep.GetCourseByID(dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString());
            //修改处理
            if (txtCourseId.Text.Trim() == String.Empty
                || txtCourseName.Text.Trim() == String.Empty
                || txtCourseCredit.Text.Trim() == String.Empty
                || comboBoxCseCat.Text.Trim() == String.Empty)
            {
                MessageBox.Show("所有字段不能为空！");
            }
            else
            {
                course.CourseId = txtCourseId.Text.Trim();
                course.CourseName = txtCourseName.Text.Trim();
                course.DepartmentId = ((Department)comboBoxDepName.Items[comboBoxDepName.SelectedIndex]).DepartmentId;
                course.CourseCredit = char.Parse(txtCourseCredit.Text.Trim());
                course.CourseCategory = comboBoxCseCat.Text.Trim();
                DialogResult result = MessageBox.Show("是否确定修改？", "修改提示", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    courseRep.Save();
                    this.frmCourseMgnt_Load(sender,e);
                }
            }
        }
        private void TstripBtnSave_Click(object sender, EventArgs e)
        {
            setDefualtStatus();
            Course course = new Course();
            if (txtCourseId.Text.Trim() == String.Empty
                || txtCourseName.Text.Trim() == String.Empty
                || txtCourseCredit.Text.Trim() == String.Empty
                || comboBoxCseCat.Text.Trim() == String.Empty)
            {
                MessageBox.Show("所有字段不能为空！");
            }
            else
            {
                course.CourseId = txtCourseId.Text.Trim();
                course.CourseName = txtCourseName.Text.Trim();
                course.DepartmentId = ((Department)comboBoxDepName.Items[comboBoxDepName.SelectedIndex]).DepartmentId;
                course.CourseCredit = char.Parse(txtCourseCredit.Text.Trim());
                course.CourseCategory = comboBoxCseCat.Text.Trim();
                //提示用户是否添加
                DialogResult result = MessageBox.Show("是否确定添加？","添加提示",MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    courseRep.Add(course);
                    courseRep.Save();
                }
            }
            this.frmCourseMgnt_Load(sender, e);
        }

        private void setDefualtStatus()
        {
            comboBoxDepName.Enabled = false;
            comboBoxCseCat.Enabled = false;
            txtCourseId.ReadOnly = true;
            txtCourseName.ReadOnly = true;
            txtCourseCredit.ReadOnly = true;
            txtDepartmentId.ReadOnly = true;
        }
        //删除操作
        private void TstripBtnDelete_Click(object sender, EventArgs e)
        {
            Course deleteCourse = courseRep.GetCourseByID(dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString());
            if (deleteCourse == null)
            {
                MessageBox.Show("该对象不存在或者已经被删除");
            }
            else
            {
                DialogResult result = MessageBox.Show("您确定删除？","删除提示",MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    courseRep.Delete(deleteCourse);
                    courseRep.Save();
                    this.frmCourseMgnt_Load(sender,e);
                }
            }
        }

        private void TstripBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellEventArgs e)
        {
            setDefualtStatus();
            if (dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value == null)
            {
                return;
            }
            else
            {
                comboBoxDepName.Text = (departRep.GetDeptByID(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString())).DepartmentName;
                txtCourseCredit.Text = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtCourseName.Text = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtCourseId.Text = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                comboBoxCseCat.Text = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtDepartmentId.Text = dataGridView1[0, dataGridView1.CurrentCellAddress.Y].Value.ToString();
            }
        }
        //在输入学分的同时进行验证，限制输入的学分在0~6之间
        private void txtCourseCredit_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (txtCourseCredit.Text.Trim() != "")
            {
                int credit = Int32.Parse(txtCourseCredit.Text.Trim());
                if (credit < 0 || credit > 6)
                {
                    if (DialogResult.OK == MessageBox.Show("学分应在0—6之间，请重新输入！"))
                    {
                        txtCourseCredit.Text = "";
                        txtCourseCredit.Focus();
                    }
                }
            }
        }

        private void TstripBtnLookUp_Click(object sender, EventArgs e)
        {
            if (TstripCboxCondition.Text.Trim() == "课程名称")
            {
                string courseName = TstripTxtKeyword.Text.Trim();
                this.dataGridView1.DataSource = courseRep.GetCourseByName(courseName);
                if(this.dataGridView1.RowCount == 0)
                    MessageBox.Show("抱歉，未搜索到你要查的结果！");
            }
            else
            {
                string departmentName = TstripTxtKeyword.Text.Trim();
                this.dataGridView1.DataSource = courseRep.GetCoursesByDName(departmentName);
                if (this.dataGridView1.RowCount == 0)
                    MessageBox.Show("抱歉，未搜索到你要查的结果！");

            }
        }

        private void comboBoxDepName_TextChanged(object sender, EventArgs e)
        {
            txtDepartmentId.Text = departRep.getDepartIdByDName(comboBoxDepName.Text.Trim());
        }

   }
}
